/* Copyright Florian Schweitzer
 * File created 04.12.2012
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
package org.openuat.android.authentication;

import java.io.IOException;

import org.openuat.android.authentication.ambientaudio.AmbientAudioPairing;
import org.openuat.android.service.DHwithVerificationImpl;
import org.openuat.android.service.RegisteredApp;
import org.openuat.android.service.RegisteredAppManager;
import org.openuat.android.service.connectiontype.IConnectionType;
import org.openuat.android.service.connectiontype.IConnectionType.CONNECTION_TYPE;
import android.util.Log;

/**
 * Class to manage the authentication session
 * concerning the connection types and
 * authentication methods
 * @author Florian Schweitzer
 *
 */
public final class AndroidAuthenticationSession {
	
	/**
	 * The AUTHENTICATION_TYPE enum
	 * @author Florian Schweitzer
	 */
	public static enum AUTHENTICATION_TYPE {

		/** The QR-Code method. */
		QRCODE,
		/** The Ambient-Audio method. */
		AMBIENTAUDIO
	};
	
	/**
	 * Starts a new authentication process
	 * @param app	The RegisteredApp-object which wants to be authenticated
	 */
	public static void startAuthenticationProcess(RegisteredApp app) {
		Log.i("AndroidAuthentication", "in startAuthenticationProcess with app " + app.getName());
		startDiscovering(app);
		
		switch (app.getAuthentication()) {
		case QRCODE:
			Log.i("AndroidAuthentication", "starting DiffieHellmanVerification");
			startDiffieHellmanVerification(app);
			break;
		case AMBIENTAUDIO:
			Log.i("AndroidAuthentication", "starting AmbientAudioPairing");
			startAmbientAudioPairing(app);
			break;
		}
	}
	
	/**
	 * Starts the discovering process
	 * @param app	The RegisteredApp-object which wants to be discovered
	 */
	public static void startDiscovering(RegisteredApp app) {
		IConnectionType.start(app.getConnection());
	}
	
	/**
	 * Starts the Diffie-Hellman verification for authentication
	 * @param app The RegisteredApp-object which wants to be authenticated
	 */
	public static void startDiffieHellmanVerification(RegisteredApp app) {
		try {
			DHwithVerificationImpl.getInstance(app.getConnection());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Starts the Ambient-Audio method for authentication
	 * @param app
	 */
	public static void startAmbientAudioPairing(RegisteredApp app) {
		try {
			AmbientAudioPairing.getInstance(app.getConnection());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Stops the authentication process (discovering and authenticating)
	 * and unregisters all registered apps
	 */
	public static void stopAuthenticationProcess() {
		boolean wifiConnUsed = RegisteredAppManager.getServicesByConnType(CONNECTION_TYPE.WIFI).size() > 0;
		boolean bluetoothConnUsed = RegisteredAppManager.getServicesByConnType(CONNECTION_TYPE.BLUETOOTH).size() > 0;
		
		boolean ambientAudioUsed = RegisteredAppManager.getServicesByAuthType(AUTHENTICATION_TYPE.AMBIENTAUDIO).size() > 0;
		boolean qrCodeUsed = RegisteredAppManager.getServicesByAuthType(AUTHENTICATION_TYPE.QRCODE).size() > 0;
		
		//Stop discovery process		
		if (wifiConnUsed) {
			IConnectionType.shutdown(CONNECTION_TYPE.WIFI);			
		}
		if (bluetoothConnUsed) {
			IConnectionType.shutdown(CONNECTION_TYPE.BLUETOOTH);
		}
		
		//Stop Authentication-Server
		if (ambientAudioUsed) {
			AmbientAudioPairing.stopServer();
		}
		if (qrCodeUsed) {
			DHwithVerificationImpl.stopServer();
		}
		
		RegisteredAppManager.unregisterAllServices();				
	}
}
